package com.ruoyi.system.service.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.WxContentEntity;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysContentMapper;
import com.ruoyi.system.service.ISysContentService;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 内容维护Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@Service
public class SysContentServiceImpl implements ISysContentService 
{
    @Autowired
    private SysContentMapper sysContentMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询内容维护
     * 
     * @param contentid 内容维护主键
     * @return 内容维护
     */
    @Override
    public SysContent selectSysContentByContentid(Long contentid)
    {
        return sysContentMapper.selectSysContentByContentid(contentid);
    }

    /**
     * 查询内容维护列表
     * 
     * @param sysContent 内容维护
     * @return 内容维护
     */
    @Override
    public List<SysContent> selectSysContentList(SysContent sysContent)
    {
        return sysContentMapper.selectSysContentList(sysContent);
    }

    /**
     * 新增内容维护
     * 
     * @param sysContent 内容维护
     * @return 结果
     */
    @Override
    public int insertSysContent(SysContent sysContent)
    {
        return sysContentMapper.insertSysContent(sysContent);
    }

    /**
     * 修改内容维护
     * 
     * @param sysContent 内容维护
     * @return 结果
     */
    @Override
    public int updateSysContent(SysContent sysContent)
    {
        return sysContentMapper.updateSysContent(sysContent);
    }

    /**
     * 批量删除内容维护
     * 
     * @param contentids 需要删除的内容维护主键
     * @return 结果
     */
    @Override
    public int deleteSysContentByContentids(Long[] contentids)
    {
        return sysContentMapper.deleteSysContentByContentids(contentids);
    }

    /**
     * 删除内容维护信息
     * 
     * @param contentid 内容维护主键
     * @return 结果
     */
    @Override
    public int deleteSysContentByContentid(Long contentid)
    {
        return sysContentMapper.deleteSysContentByContentid(contentid);
    }
    /**
     * 查询内容维护列表 微信专用
     *

     * @return 内容维护集合
     */
    @Override
    public List<SysContent> selectSysContentListByWx(WxContentEntity wxContentEntity){
        //判断是什么资源
        if(wxContentEntity.getQueryParam()!=null&&!wxContentEntity.getQueryParam().isEmpty()){
            //查询的是加密资源
        }
        return sysContentMapper.selectSysContentListByWx(wxContentEntity);
    }

    @Override
    public UnionRespose unionlist( String unionid,   String version){

        String cachekey=CacheConstants.SYS_UNIONAPP_KEY+unionid.trim()+"tagunion"+version.trim();
        //判断是否存在该key  如果存在则直接返回即可
       if (redisCache.hasKey(cachekey)){
           System.out.println("我从缓存中来********");
           return  redisCache.getCacheObject(cachekey);
       }
       //获取   检查是否为  测试版本
        List<Map<String, Object>> tt=   jdbcTemplate.queryForList(" SELECT version  FROM sys_union_versiont  where ownerid ='"+unionid.trim()+"'");
         boolean isCeshi=true;
           if (tt!=null&&tt.size()>0){
                int dbVersion= (int) tt.get(0).get("version");
                //对比版本，如果数据库版本小于  请求版本，则表示是测试，否则未正式
               int httpVersion=Integer.parseInt(version.trim());
               if (dbVersion>=httpVersion){
                   isCeshi=false;
               }
            }
           //如果是测试-----
        UnionRespose respose=new UnionRespose();
        List<UnionInnerBean>resultList=new ArrayList<>();

        if (isCeshi){
            //获取测试公告  取状态禁用的公告  数据即可
            List<Map<String, Object>> step2List=   jdbcTemplate.queryForList(" SELECT content,server_pic FROM sys_notice_service where status=1 ORDER BY updatedate desc");
           if (step2List!=null&&step2List.size()>0){
                String content=   step2List.get(0).get("content").toString();
               String server_pic=   step2List.get(0).get("server_pic").toString();
               respose.setHomepic(server_pic);
               respose.setNotice(content);
            }
           //接下来展示数据
            List<Map<String, Object>> step33List=   jdbcTemplate.queryForList("SELECT title,name ,name2,content,pic_path FROM sys_union_content  where content_owner is null and status=1 ORDER BY create_time desc ");
            if (step33List!=null&&step33List.size()>0){
                for (int f=0;f<step33List.size();f++){
                    UnionInnerBean unionInnerBean=new UnionInnerBean();
                    unionInnerBean.setContent( step33List.get(f).get("content").toString());
                    unionInnerBean.setName1( step33List.get(f).get("name").toString());
                    unionInnerBean.setName2( step33List.get(f).get("name2").toString());
                    unionInnerBean.setTitle( step33List.get(f).get("title").toString());
                    unionInnerBean.setPic( step33List.get(f).get("pic_path").toString());
                    resultList.add(unionInnerBean);
                }

            }

        }else {
            //获取测试公告     数据即可
            List<Map<String, Object>> step2List=   jdbcTemplate.queryForList(" SELECT content,server_pic FROM sys_notice_service where status=0 ORDER BY updatedate desc");
            if (step2List!=null&&step2List.size()>0){
                String content=   step2List.get(0).get("content").toString();
                String server_pic=   step2List.get(0).get("server_pic").toString();
                respose.setHomepic(server_pic);
                respose.setNotice(content);
            }
            //接下来展示数据
            List<Map<String, Object>> step33List=   jdbcTemplate.queryForList("SELECT title,name ,name2,content,pic_path FROM sys_union_content  where content_owner='"+unionid.trim()+"' and status=0 ORDER BY create_time desc ");
            if (step33List!=null&&step33List.size()>0){
                for (int f=0;f<step33List.size();f++){
                    UnionInnerBean unionInnerBean=new UnionInnerBean();
                    unionInnerBean.setContent( step33List.get(f).get("content").toString());
                    unionInnerBean.setName1( step33List.get(f).get("name").toString());
                    unionInnerBean.setName2( step33List.get(f).get("name2").toString());
                    unionInnerBean.setTitle( step33List.get(f).get("title").toString());
                    unionInnerBean.setPic( step33List.get(f).get("pic_path").toString());
                    resultList.add(unionInnerBean);
                }

            }
        }
        respose.setList(resultList);
        if (isCeshi){
            //存入缓存中
            redisCache.setCacheObject(cachekey,respose);
        }

        return respose;
    }

    @Override
    public List<SysContent> selectSysContentListByWxcszy(WxContentEntity wxContentEntity) {
        return sysContentMapper.selectSysContentListByWxcszy(wxContentEntity);
    }

    @Override
    public boolean checkResourceIsExist(String resourceId) {
        List<Map<String, Object>> tt=   jdbcTemplate.queryForList(" SELECT 1 FROM sys_needpull_data where resourceid ='"+resourceId+"'");
        if (tt!=null&&tt.size()>0){
            return true;
        }
//        String yqrmember= (String) map.get("attribute1");
//        int groupid= (int) map.get("groupid");
//        List tt=   jdbcTemplate.queryForList(" SELECT 1 FROM sys_wx_group where    FIND_IN_SET('"+yqrmember+"',member) and id= "+groupid);
//        if (tt!=null&&tt.size()>0){
//
//        }
        return false;
    }

    @Override
    public void inputResource(List<NeedInputBean> needInputBeanList) {
        batchInsertNeedinput(needInputBeanList);

    }

    @Override
    public void updateNeedPullToSucess(String resourceId) {
        jdbcTemplate.execute("update  sys_needpull_data set   status =1 where resourceid='"+resourceId+"'");

    }

    @Override
    public void resetConfigCache() {
        Collection<String> keys = redisCache.keys(CacheConstants.SYS_UNIONAPP_KEY + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 批量插入数据
     * @param
     * @param
     * @return
     */
    public int batchInsertNeedinput(List<NeedInputBean> needInputBeanList ) {
        String insertStockOrderItemBarcodeSql = "INSERT INTO sys_needpull_data (resourceid, show_put_time) VALUES  (?, ?)";
        int[] updateCountArray = jdbcTemplate.batchUpdate(insertStockOrderItemBarcodeSql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1, needInputBeanList.get(i).getResourceId());
                ps.setDate(2, new java.sql.Date(needInputBeanList.get(i).getShowPutTime().getTime())  );
            }
            @Override
            public int getBatchSize() {
                return needInputBeanList.size();
            }
        });
        int sumInsertedCount = 0;
        for (int a : updateCountArray) {
            sumInsertedCount += a;
        }
        return sumInsertedCount;
    }
}
